//
//  LevelManager.m
//  Project 5
//
//  Created by Nguyen Tan Loc on 6/20/13.
//  Copyright (c) 2013 Nguyen Tan Loc. All rights reserved.
//

#import "LevelManager.h"
#import "DefineXML.h"
#import "Utility.h"

@implementation LevelRequire
@synthesize _type = mType;
@synthesize _value = mValue;

@end

@implementation LevelDetail
@synthesize _energy = mEnergy;
@synthesize _score = mScore;
@synthesize _star = mStar;
@synthesize _time = mTime;

-(void) encodeWithCoder:(NSCoder *)encoder
{
    [encoder encodeInt:mEnergy forKey:KEY_LEVEL_DETAIL_ENERGY];
    [encoder encodeInt:mScore forKey:KEY_LEVEL_DETAIL_SCORE];
    [encoder encodeInt:mStar forKey:KEY_LEVEL_DETAIL_STAR];
    [encoder encodeFloat:mTime forKey:KEY_LEVEL_DETAIL_TIME];
}

-(id) initWithCoder:(NSCoder *)decoder
{
    if (self = [super init])
    {
        mEnergy = [decoder decodeIntForKey:KEY_LEVEL_DETAIL_ENERGY];
        mScore = [decoder decodeIntForKey:KEY_LEVEL_DETAIL_SCORE];
        mStar = [decoder decodeIntForKey:KEY_LEVEL_DETAIL_STAR];
        mTime = [decoder decodeFloatForKey:KEY_LEVEL_DETAIL_TIME];
    }

    return self;
}

@end


// ---------------------------------------------
@implementation LevelManager
@synthesize _numEnergy = mNumEnergy;
@synthesize _levelTime = mLevelTime;
@synthesize _currentLevel = mCurrentLevel;
@synthesize _maxNumEnergy = mMaxNumEnergy;
@synthesize _itemMagnetTime = mItemMagnetTime;
@synthesize _itemShieldTime = mItemShieldTime;
@synthesize _isDarkFog = mIsDarkFog;
@synthesize _maxReachLevel = mMaxReachLevel;
@synthesize _isNewLevel = mIsNewLevel;
@synthesize _isStartNow = mIsStartNow;

static LevelManager *m_Instance = nil;

+(LevelManager *) getInstance
{
    if (m_Instance == nil)
        m_Instance = [[LevelManager alloc] init];
    return m_Instance;
}

-(id) init
{
    if (self = [super init])
    {
        mListLevel = [[NSArray alloc] initWithObjects:
                      @"level1",
                      @"level2",
                      @"level3",
                      @"level4",
                      @"level5",
                      @"level6",
                      @"level7",
                      @"level8",
                      @"level9",
                      @"level10",
                      @"level11",
                      @"level12",
                      @"level13",
                      @"level14",
                      @"level15",
                      @"level16",
                      @"level17",
                      @"level18",
                      @"level19",
                      @"level20",
                      @"level21",
                      @"level22",
                      @"level23",
                      @"level24",
                      @"level25",
                      @"level26",
                      @"level27",
                      @"level28",
                      @"level29",
                      @"level30",
                      @"level31",
                      @"level32",
                      @"level33",
                      @"level34",
                      @"level35",
                      @"level36",
                      @"level37",
                      @"level38",
                      @"level39",
                      @"level40",
                      @"level41",
                      @"level42",
                      @"level43",
                      @"level44",
                      @"level45",
                      @"level46",
                      @"level47",
                      @"level48",
                      @"level49",
                      @"level50",
                      @"level51",
                      @"level52",
                      @"level53",
                      @"level54",
                      @"level55",
                      @"level56",
                      @"level57",
                      @"level58",
                      @"level59",
                      @"level60",
                      @"level61",
                      @"level62",
                      @"level63",
                      @"level64",
                      @"level65",
                      @"level66",
                      @"level67",
                      @"level68",
                      @"level69",
                      @"level70",
                      @"level71",
                      @"level72",
                      @"level73",
                      @"level74",
                      @"level75",
                      @"level76",
                      @"level77",
                      @"level78",
                      @"level79",
                      @"level80",
                      @"level81",
                      @"level82",
                      @"level83",
                      @"level84",
                      @"level85",
                      @"level86",
                      @"level87",
                      @"level88",
                      @"level89",
                      @"level90",
                      @"level91",
                      @"level92",
                      @"level93",
                      @"level94",
                      @"level95",
                      @"level96",
                      @"level97",
                      @"level98",
                      @"level99",
                      @"level100",
                      nil];
        
        mListLevelRequire = [[NSMutableArray alloc] init];
        mIsNewLevel = YES;
        mCurrentLevel = -1;
        [self getListLevelDetail];
        [self getMaxReachLevel];
    }
    
    return self;
}

-(BOOL) isAvailableNextLevel
{
    if ((mCurrentLevel + 1) >= mListLevel.count)
        return NO;
    return YES;
}

-(void) getListLevelDetail
{
    mListLevelDetail = [[NSMutableArray alloc] init];
    for (int i = 0; i < mListLevel.count; i++)
    {
        LevelDetail *levelDetail = [[Utility getInstance] loadObjectForKey:[mListLevel objectAtIndex:i]];
        if (levelDetail == nil)
            levelDetail = [[[LevelDetail alloc] init] autorelease];
        [mListLevelDetail addObject:levelDetail];
        
//        CCLOG(@"level = %d, time = %f, energy = %d, score = %d, star = %d", i, levelDetail._time, 
//            levelDetail._energy, levelDetail._score, levelDetail._star);
    }
}

-(LevelDetail *) getLevelDetail:(int)level
{
    return [mListLevelDetail objectAtIndex:level];
}

-(LevelDetail *) getCurrentLevelDetail
{
    return [self getLevelDetail:mCurrentLevel];
}

-(NSString *) getLevelName:(int)level
{
    if (level >= mListLevel.count)
        NSLog(@"GetLevelName - LevelManager: Index out of array");
    return [mListLevel objectAtIndex:level];
}

-(NSString *) getCurrentLevelName
{
    return [self getLevelName:mCurrentLevel];
}

-(void) loadCurrentLevel
{
    if (mCurrentLevel >= mListLevel.count)
    {
        CCLOG(@"level is out of range");
    }
    
    NSString *path = [mListLevel objectAtIndex:mCurrentLevel];
    [self loadLevel:path];
}

-(void) loadLevel:(NSString *)path
{
    [self resetCurrentLevelInfo];
    
    NSString *filePath = [[NSBundle mainBundle] pathForResource:path ofType:@"xml"];
    NSData *xmlData = [[NSMutableData alloc] initWithContentsOfFile:filePath];
    NSError *error;
    
    mDataXMLDocument = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:&error];
    
    if (mDataXMLDocument == nil)
    {
        // process here
        CCLOG(@"document = nil");
    }
    
    [xmlData release];
    xmlData = nil;
    
    [self loadlevelInfo];
}

-(void) loadlevelInfo
{
    if ([mListLevelRequire count] > 0)
       [mListLevelRequire removeAllObjects];
    
    NSArray *listLevelInfo = [mDataXMLDocument.rootElement elementsForName:XML_LEVEL_INFO];
    for (GDataXMLElement *eInfo in listLevelInfo)
    {
        mLevelTime = [[[eInfo attributeForName:XML_TIME] stringValue] floatValue];
        mIsDarkFog = [[[eInfo attributeForName:XML_DARK_FOG] stringValue] boolValue];
        mIsStartNow = ![[[eInfo attributeForName:XML_IS_START_LATER] stringValue] boolValue];
        mItemAvailable = [[eInfo attributeForName:XML_ITEM] stringValue];
        
        NSArray *listRequireInfo = [eInfo elementsForName:XML_REQUIRE];
        for (GDataXMLElement *eRequire in listRequireInfo)
        {
            LevelRequire *require = [[[LevelRequire alloc] init] autorelease];
            require._type = (eLevelRequireType)[[[eRequire attributeForName:XML_TYPE] stringValue] intValue];
            require._value = [[[eRequire attributeForName:XML_VALUE] stringValue] intValue];
//            CCLOG(@"type = %d", (int)require._type);
            [mListLevelRequire addObject:require];
        }
    }
}

-(NSArray *) getListPath
{
    NSMutableArray *listPath = [NSMutableArray array];
    NSArray *listPathInfo = [mDataXMLDocument.rootElement elementsForName:XML_LINE_PATH];
    for (GDataXMLElement *ePath in listPathInfo)
    {
        NSMutableArray *listNode = [[NSMutableArray alloc] init];
        NSArray *listNodeInfo = [ePath elementsForName:XML_NODE];
        float spreadTime = [[[ePath attributeForName:XML_SPREAD_TIME] stringValue] floatValue];
        float unX = [[[ePath attributeForName:XML_X] stringValue] floatValue];
        float unY = [[[ePath attributeForName:XML_Y] stringValue] floatValue];
        
        PATH_LINE_SPREAD_SPEED = [[[ePath attributeForName:XML_SPREAD_SPEED] stringValue] floatValue];
        PATH_LINE_SPREAD_SPEED = uniPos(PATH_LINE_SPREAD_SPEED, PATH_LINE_SPREAD_SPEED * 2);
        
        int tag = 0;
        for (GDataXMLElement *eNode in listNodeInfo)
        {
            NSArray *listActionInfo = [eNode elementsForName:XML_ACTION];
            float x = [[[eNode attributeForName:XML_X] stringValue] floatValue];
            float y = [[[eNode attributeForName:XML_Y] stringValue] floatValue];
            
            PNode *node = [[PNode alloc] initWithType:eNT_PATH_NODE];
            node._state = (tag == 0) ? eNS_CONNECTING : eNS_IDLE;
            node.tag = tag++;
            [node setIsUseBox2xAnchorPoint:YES];
            [node setAnchorPoint:ccp(0.5, 0.5)];
            [node setPosition:unicpp(x, y)];
            [self loadAction:node listActionInfo:listActionInfo];
            
            [listNode addObject:node];
            [node release];
        }
        
        Path *path = [[Path alloc] initWithList:listNode];
        [path setSpreadTime:spreadTime];
        [[path getUserNode] setPosition:unicpp(unX, unY)];
        
        if (spreadTime > 0)
        {
            float virusX = [[[ePath attributeForName:XML_VX] stringValue] floatValue];
            float virusY = [[[ePath attributeForName:XML_VY] stringValue] floatValue];
            [path setVirusPos:unicpp(virusX, virusY)];
        }
        
        [listPath addObject:path];
        [path release];
        [listNode release];
    }
    
    return listPath;
}

-(NSArray *) getListLazer
{
    NSMutableArray *listLazer = [NSMutableArray array];
    NSArray *listLazerInfo = [mDataXMLDocument.rootElement elementsForName:XML_LAZER_PATH];
    for (GDataXMLElement *ePath in listLazerInfo)
    {
        NSMutableArray *listNode = [[NSMutableArray alloc] init];
        NSArray *listNodeInfo = [ePath elementsForName:XML_NODE];
        
        
        for (GDataXMLElement *eNode in listNodeInfo)
        {
            NSArray *listActionInfo = [eNode elementsForName:XML_ACTION];
            float x = [[[eNode attributeForName:XML_X] stringValue] floatValue];
            float y = [[[eNode attributeForName:XML_Y] stringValue] floatValue];
            
            LNode *node = [[LNode alloc] init];
            [node setIsUseBox2xAnchorPoint:YES];
            [node setIgnoreBodyRotation:NO];
            [node setAnchorPoint:ccp(0.5, 0.5)];
            [node setPosition:unicpp(x, y)];
            [self loadAction:node listActionInfo:listActionInfo];
            
            [listNode addObject:node];
            [node release];
        }
        
        float delay = [[[ePath attributeForName:XML_DELAY] stringValue] floatValue];
        float ontime = [[[ePath attributeForName:XML_ON_TIME] stringValue] floatValue];
        float offtime = [[[ePath attributeForName:XML_OFF_TIME] stringValue] floatValue];
        
        Lazer *lazer = [[Lazer alloc] initWithList:listNode];
        [lazer setTimeDelay:delay timeOn:ontime timeOff:offtime];
        
        [listLazer addObject:lazer];
        [lazer release];
        [listNode release];
    }
    
    return listLazer;
}

-(NSArray *) getListBomb
{
    NSMutableArray *listBomb = [NSMutableArray array];
    NSArray *listBombInfo = [mDataXMLDocument.rootElement elementsForName:XML_BOMB];
    for (GDataXMLElement *eBomb in listBombInfo)
    {
        NSArray *listActionInfo = [eBomb elementsForName:XML_ACTION];
        float x = [[[eBomb attributeForName:XML_X] stringValue] floatValue];
        float y = [[[eBomb attributeForName:XML_Y] stringValue] floatValue];
        
        Bomb *bomb = [[Bomb alloc] init];
        [bomb setIsUseBox2xAnchorPoint:YES];
        [bomb setAnchorPoint:ccp(0.5, 0.5)];
        [bomb setPosition:unicpp(x, y)];
        [self loadAction:bomb listActionInfo:listActionInfo];
        
        // for test
//        [bomb startAction];
        
        [listBomb addObject:bomb];
        [bomb release];
    }
    
    return listBomb;
}

-(NSArray *) getListEnergy
{
    NSMutableArray *listEnergy = [NSMutableArray array];
    NSArray *listEnergyInfo = [mDataXMLDocument.rootElement elementsForName:XML_ENERGY];
    int numEnergy = 0;
    for (GDataXMLElement *eEnergy in listEnergyInfo)
    {
        NSArray *listActionInfo = [eEnergy elementsForName:XML_ACTION];
        float x = [[[eEnergy attributeForName:XML_X] stringValue] floatValue];
        float y = [[[eEnergy attributeForName:XML_Y] stringValue] floatValue];
        
        Energy *energy = [[Energy alloc] init];
        [energy setIsUseBox2xAnchorPoint:YES];
        [energy setAnchorPoint:ccp(0.5, 0.5)];
        [energy setPosition:unicpp(x, y)];
        [self loadAction:energy listActionInfo:listActionInfo];
        
        [listEnergy addObject:energy];
        [energy release];
        
        numEnergy++;
    }
    
    mMaxNumEnergy = numEnergy;
    
    return listEnergy;
}

-(NSArray *) getListBouncBall
{
    NSMutableArray *listBouncBall = [NSMutableArray array];
    NSArray *listBouncBallInfo = [mDataXMLDocument.rootElement elementsForName:XML_BOUNC_BALL];
    for (GDataXMLElement *eBouncBall in listBouncBallInfo)
    {
        float x = [[[eBouncBall attributeForName:XML_X] stringValue] floatValue];
        float y = [[[eBouncBall attributeForName:XML_Y] stringValue] floatValue];
        float vx = [[[eBouncBall attributeForName:XML_VX] stringValue] floatValue];
        float vy = [[[eBouncBall attributeForName:XML_VY] stringValue] floatValue];
        float time = [[[eBouncBall attributeForName:XML_TIME] stringValue] floatValue];
        int type = [[[eBouncBall attributeForName:XML_TYPE] stringValue] intValue];
        
        BouncingBall *bouncBall = [[BouncingBall alloc] init];
        [bouncBall setPosition:unicpp(x, y)];
        bouncBall._Vx = uniPos(vx, vx * 2);
        bouncBall._Vy = uniPos(vy, vy * 2);
        bouncBall._bossType = (eBoss_Type)type;
        bouncBall._timeBomb = time;
        [[bouncBall getEye] setPosition:bouncBall.position];
        
        [listBouncBall addObject:bouncBall];
        [bouncBall release];
    }
    
    return listBouncBall;
}

-(NSArray *) getListItem
{
    return nil;
}

-(NSArray *) getListDecor
{
    return nil;
}

-(NSArray *) getListTutorial
{
    NSMutableArray *listTutorial = [NSMutableArray array];
    NSArray *listTutorialInfo = [mDataXMLDocument.rootElement elementsForName:XML_TUTORIAL];
    for (GDataXMLElement *eTutorial in listTutorialInfo)
    {
        CCNode<CCRGBAProtocol> *node = nil;
        NSString *type = [[eTutorial attributeForName:XML_TYPE] stringValue];
        float x = [[[eTutorial attributeForName:XML_X] stringValue] floatValue];
        float y = [[[eTutorial attributeForName:XML_Y] stringValue] floatValue];
        int opacity = [[[eTutorial attributeForName:XML_OPACITY] stringValue] intValue];
        int loop = [[[eTutorial attributeForName:XML_LOOP] stringValue] intValue];
        
        if ([type isEqualToString:XML_IMAGE])
        {
            NSString *frameName = [[eTutorial attributeForName:XML_FRAME] stringValue];
            
            
            node = [CCSprite spriteWithSpriteFrameName:frameName];
            [node setPosition:unicpp(x, y)];
            
        }
        else if ([type isEqualToString:XML_TEXT])
        {
            NSString *text = [[eTutorial attributeForName:XML_TEXT] stringValue];
            node = [CCLabelBMFont labelWithString:text fntFile:FONT_DEFAULT];
            [node setAnchorPoint:ccp(0.5, 0.5)];
            
        }
        
        NSArray *listActionInfo = [eTutorial elementsForName:XML_ACTION];
        [self loadTutorialAction:node listActionInfo:listActionInfo loop:loop];
        [node setPosition:unicpp(x, y)];
        
        [node setOpacity:opacity];
        [listTutorial addObject:node];
    }
    
    return listTutorial;
}

-(void) loadTutorialAction:(CCNode<CCRGBAProtocol> *)object listActionInfo:(NSArray *)listActionInfo loop:(int)loop
{
    if (listActionInfo.count == 0)
        return;
    
    NSMutableArray *listAction = [NSMutableArray array];
    CGPoint lastPosition = object.position;
    
    for (GDataXMLElement *eAction in listActionInfo)
    {
        NSString *actionType = [[eAction attributeForName:XML_TYPE] stringValue];
        id action = nil;
        
        if ([actionType isEqualToString:XML_DELAY])
        {
            float time = [[[eAction attributeForName:XML_TIME] stringValue] floatValue];
            action = [CCDelayTime actionWithDuration:time];
        }
        else if ([actionType isEqualToString:XML_FADE])
        {
            int opacity = [[[eAction attributeForName:XML_OPACITY] stringValue] intValue];
            float time = [[[eAction attributeForName:XML_TIME] stringValue] floatValue];
            action = [CCFadeTo actionWithDuration:time opacity:opacity];
        }
        else if ([actionType isEqualToString:XML_MOVE_TO])
        {
            float x = [[[eAction attributeForName:XML_X] stringValue] floatValue];
            float y = [[[eAction attributeForName:XML_Y] stringValue] floatValue];
            float speed = [[[eAction attributeForName:XML_SPEED] stringValue] floatValue];
            speed = uniPos(speed, speed * 2);
            CGPoint nextPosition = unicpp(x, y);
            float time = ccpLength(ccpSub(nextPosition, lastPosition)) / speed;
            
            lastPosition = nextPosition;
            action = [CCMoveTo actionWithDuration:time position:nextPosition];
        }
        else if ([actionType isEqualToString:XML_POSITION])
        {
            float x = [[[eAction attributeForName:XML_X] stringValue] floatValue];
            float y = [[[eAction attributeForName:XML_Y] stringValue] floatValue];
            
            action = [CCMoveTo actionWithDuration:0 position:unicpp(x, y)];
        }
        
        [listAction addObject:action];
    }
    
    CCSequence *sequence = [CCSequence actionWithArray:listAction];
    CCRepeat *repeat = [CCRepeat actionWithAction:sequence times:loop];
    [object runAction:repeat];
}

-(void) loadAction:(CCSprite<DynamicObjectInterface> *)object listActionInfo:(NSArray *)listActionInfo
{
    NSMutableArray *listAction = [NSMutableArray array];
    for (GDataXMLElement *eAction in listActionInfo)
    {
        int actionType = [[[eAction attributeForName:XML_TYPE] stringValue] intValue];
        if (actionType == ACTION_TYPE_ROTATE)
        {
            float radius = [[[eAction attributeForName:XML_RADIUS] stringValue] floatValue];
            float startAngle = [[[eAction attributeForName:XML_START_ANGLE] stringValue] floatValue];
            float speedRotate = [[[eAction attributeForName:XML_SPEED] stringValue] floatValue];
            radius = uniPos(radius, radius * 2);

            [object setAnchorPoint:ccp(0.5, -(radius / object.contentSize.height) + 0.5)];
            [object setRotation:startAngle];
            
            CCRotateBy *rotateBy = [CCRotateBy actionWithDuration:1 angle:speedRotate];
            CCRepeatForever *repeat = [CCRepeatForever actionWithAction:rotateBy];
            [listAction addObject:repeat];
        }
        else if (actionType == ACTION_TYPE_ROTATE_A)
        {
            float ax = [[[eAction attributeForName:XML_ANCHOR_X] stringValue] floatValue];
            float ay = [[[eAction attributeForName:XML_ANCHOR_Y] stringValue] floatValue];
            float speedRotate = [[[eAction attributeForName:XML_SPEED] stringValue] floatValue];
            BOOL delta = [[[eAction attributeForName:XML_DELTA] stringValue] boolValue];
            
            CGPoint sub;
            if (delta == YES) sub = ccp(ax, ay);
            else sub = ccpSub(unicpp(ax, ay), object.position);
            
            float startAngle = CC_RADIANS_TO_DEGREES(atan2f(-sub.y, sub.x));
            float radius = ccpLength(sub);
            float startAngle1 = [[[eAction attributeForName:XML_START_ANGLE] stringValue] floatValue];
            startAngle += startAngle1;
            
            [object setAnchorPoint:ccp(0.5, -(radius / object.contentSize.height) + 0.5)];
            [object setRotation:startAngle];
            
            CCRotateBy *rotateBy = [CCRotateBy actionWithDuration:1 angle:speedRotate];
            CCRepeatForever *repeat = [CCRepeatForever actionWithAction:rotateBy];
            [listAction addObject:repeat];
        }
        else if (actionType == ACTION_TYPE_MOVE_TO)
        {
            NSMutableArray *listMoveTo = [NSMutableArray array];
            NSMutableArray *listActionMoveTo = [NSMutableArray array];
            NSArray *listMoveToInfo = [eAction elementsForName:XML_MOVE_TO];
            int easeSin = [[[eAction attributeForName:XML_EASE_SINE] stringValue] intValue];
            
            for (GDataXMLElement *eMoveTo in listMoveToInfo)
            {
                float x = [[[eMoveTo attributeForName:XML_X] stringValue] floatValue];
                float y = [[[eMoveTo attributeForName:XML_Y] stringValue] floatValue];
                float speed = [[[eMoveTo attributeForName:XML_SPEED] stringValue] floatValue];
                
                [listMoveTo addObject:[NSValue valueWithCGPoint:unicpp(x, y)]];
                [listMoveTo addObject:[NSNumber numberWithFloat:uniPos(speed, speed * 2)]];
            }
            
            float lastX = object.position.x;
            float lastY = object.position.y;
            for (int i = 0; i < listMoveTo.count; i += 2)
            {
                CGPoint currentPoint = ccp(lastX, lastY);
                CGPoint nextPoint = [[listMoveTo objectAtIndex:i] CGPointValue];
                float speed = [[listMoveTo objectAtIndex:i + 1] floatValue];
                float time = ccpLength(ccpSub(nextPoint, currentPoint)) / speed;
    
                CCMoveTo *moveTo = [CCMoveTo  actionWithDuration:time position:nextPoint];
                if (easeSin == 1)
                {
                    moveTo = [CCEaseSineInOut actionWithAction:moveTo];
                }
                [listActionMoveTo addObject:moveTo];
                
                lastX = nextPoint.x;
                lastY = nextPoint.y;
            }
            
            int loop = [[[eAction attributeForName:XML_LOOP] stringValue] intValue];
            if (loop == 0)
                loop = INT_MAX;
            
            
            CCSequence *sequence = [CCSequence actionWithArray:listActionMoveTo];
            CCAction *repeat = [CCRepeat actionWithAction:sequence times:loop];
            
            float delayTime = [[[eAction attributeForName:XML_DELAY] stringValue] floatValue];
            if (delayTime > 0)
            {
                CCDelayTime *delay = [CCDelayTime actionWithDuration:delayTime];
                repeat = [CCSequence actions:delay, repeat, nil];
            }
            
            [listAction addObject:repeat];
        }
    }

    if (listAction.count > 0)
    {
        [object addActions:listAction];
    }
}

-(void) resetCurrentLevelInfo
{
    mNumEnergy = 0;
    mItemMagnetTime = 0;
    mItemShieldTime = 0;
}

-(BOOL) isItemAvailable:(ITEM_TYPE)type
{
    int index = (int)type;
    unichar c = [mItemAvailable characterAtIndex:index];
    if (c == '1')
        return YES;
    return NO;
}

-(int) getNumItemAvailable
{
    int count = 0;
    for (int i = 0; i < mItemAvailable.length; i++)
    {
        unichar c = [mItemAvailable characterAtIndex:i];
        if (c == '1')
            count++;
    }
    
    return count;
}

-(NSArray *) getListLevelRequire
{
    return mListLevelRequire;
}

-(int) getNumLevel
{
    return [mListLevel count];
}

-(void) removeData
{
    [mDataXMLDocument release];
    mDataXMLDocument = nil;
}

-(void) saveMaxReachLevel
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setInteger:mMaxReachLevel forKey:KEY_LEVEL_MAX_REACH];
    [defaults synchronize];
}

-(void) getMaxReachLevel
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    mMaxReachLevel = [defaults integerForKey:KEY_LEVEL_MAX_REACH];
}

-(int) getNumStar
{
    int count = 0;
    for (int i = 0; i < mListLevelDetail.count; i++)
    {
        LevelDetail *detail = [mListLevelDetail objectAtIndex:i];
        count += detail._star;
    }
    
    return count;
}

-(int) getNumMaxStar
{
    return (mListLevel.count * 3);
}

-(void) unlockNewLevel
{
    if (mMaxReachLevel < [self getNumLevel] && mMaxReachLevel == mCurrentLevel)
    {
        mMaxReachLevel++;
        [self saveMaxReachLevel];
    }
}

-(ccColor3B) getWorldColor
{
    return [self getWorldColorWithLevel:mCurrentLevel];
}

-(ccColor3B) getWorldColorWithLevel:(int)level
{
    if (mCurrentLevel < 20)
    {
        return CL_PANEL_BTN;
    }
    else if (mCurrentLevel < 40)
    {
        return CL_RED;
    }
    else
    {
        return CL_BLUE;
    }
    
    return CL_BLUE;
}

@end


